from django.db import models
from django.forms import ModelForm

# Create your models here.
GENDER_CHOICE = (('M','Male'),('F','Female'))

MATCH_STATUS = (
                (1,'Scheduled'),
                (2,'Played'),
                (3,'Delayed'),
                (4,'Suspended'))

PITCH_TYPES = (
                 (1,'Grass'),
                 (2,'Sand'),
                 (3,'Water'),
                 (4,'Hybrid'))
# ****************************************************************************
# extra models
class Country(models.Model):
    name = models.CharField(max_length=40)
    
    def __unicode__(self):
        return self.name
    
class ZipCode(models.Model):
    zip_code = models.IntegerField()
    city = models.CharField(max_length=40)
    state = models.CharField(max_length=30)
    country = models.ForeignKey(Country)
    
    class Meta:
        unique_together = ('zip_code','state','country')
        
    def __unicode__(self):
        return self.city

class Person(models.Model):
    id_code = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=30)
    surname = models.CharField(max_length=30)
    genre = models.CharField(max_length=1,choices=GENDER_CHOICE)
    birth = models.DateField()
    birth_country = models.CharField(max_length=30)
    nationality = models.ForeignKey(Country)
    
    class Meta:
        abstract = True
    
# ****************************************************************************

class ExtraSetup(models.Model):
    code = models.CharField(primary_key=True,max_length=30)
    value = models.CharField(max_length=100)
    description = models.CharField(max_length=200)
    
class Federation(models.Model):
    fed_code = models.CharField(primary_key=True, max_length=15)
    
class HMSSetup(models.Model):
    '''
    Two records allowed: SETUP and BACKUP
    '''
    code = models.CharField(primary_key=True,max_length=10)
    major_federation = models.ForeignKey(Federation)
    
class RefereeLevel(models.Model):
    '''
    Low level = 1
    High level = 20
    
    intial setup could be (1,5,10,15,20) and if more levels are demanded can be added between them
    '''
    ref_level = models.IntegerField(primary_key=True)
    description = models.CharField(max_length=20)
    
    def __unicode__(self):
        return self.description
    
class Category(models.Model):
    category_code = models.CharField(primary_key=True,max_length=10)
    description = models.CharField(max_length=30)
    genre = models.CharField(max_length=1,choices=GENDER_CHOICE)
    referee_level = models.ForeignKey(RefereeLevel,blank=True,null=True,on_delete=models.SET_NULL)
    
    class Meta:
        ordering = ['genre','-referee_level']
        
    def __unicode__(self):
        return self.description
        
    @models.permalink
    def get_absolute_url(self):
        return ('category_challengues', '', {'categ':self.category_code})
        
class Club(models.Model):
    club_code = models.CharField(primary_key=True, max_length=10)
    club_name = models.CharField(max_length=50)
    address = models.CharField(max_length=50)
    zip_code = models.ForeignKey(ZipCode,default='',blank=True,verbose_name='City')
    email = models.EmailField()
    phone = models.CharField(max_length=30,blank=True)
    
    def __unicode__(self):
        return self.club_name
        
    @models.permalink
    def get_absolute_url(self):
        return ('view_club', (), {'clubCode':self.club_code})

class Team(models.Model):
    year_on_competition = models.IntegerField()
    club = models.ForeignKey(Club,blank=True,null=True,on_delete=models.SET_NULL)
    category = models.ForeignKey(Category,blank=True,null=True,on_delete=models.SET_NULL)
    remark = models.CharField(max_length=10) # same club-category may have different teams
    
    def __unicode__(self):
        return u'%s - %s' % (self.club, self.category)
    
class Player(Person):
    club = models.ForeignKey(Club,blank=True,null=True,on_delete=models.SET_NULL)
    category = models.ForeignKey(Category)
    
    def __unicode__(self):
        return u'%s - %s %s' % (self.club,self.name,self.surname)
        
class PlayerSigning(models.Model):
    player = models.ForeignKey(Player)
    year_signed = models.IntegerField()
    signed_date = models.DateField()
    
    class Meta:
        unique_together = ('player','year_signed')
        
    def __unicode__(self):
        return u'%s %s - %d' % (self.player.name,self.player.surname)
    
class Referee(Person):
    referee_level = models.ForeignKey(RefereeLevel,blank=True,null=True,on_delete=models.SET_NULL)
    
    def __unicode__(self):
        return u'%s %s' % (self.name,self.surname)
    
class Tournament(models.Model):
    name = models.CharField(max_length=45)
    tournamentYear = models.DateField()
    category = models.ForeignKey(Category,blank=True,null=True,on_delete=models.SET_NULL)
    regulation = models.TextField()
    active = models.BooleanField()
    
    def __unicode__(self):
        return self.name
        
class TournamentPhase(models.Model):
    name = models.CharField(max_length=30)
    tournament = models.ForeignKey(Tournament)
    
    def __unicode__(self):
        return self.name
    
class HockeyField(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=50)
    zip_code = models.ForeignKey(ZipCode,blank=True,null=True,on_delete=models.SET_NULL,verbose_name='City')
    pitch= models.IntegerField(choices=PITCH_TYPES)
    club = models.ForeignKey(Club,blank=True,null=True,on_delete=models.SET_NULL)
    
    def __unicode__(self):
        return self.name
    
class Match(models.Model):
    category = models.ForeignKey(Category,blank=True,null=True,on_delete=models.SET_NULL)
    tournament = models.ForeignKey(Tournament)
    tournament_phase = models.ForeignKey(TournamentPhase)
    match_date = models.DateField()
    match_time = models.TimeField()
    hockey_field = models.ForeignKey(HockeyField)
    match_status = models.IntegerField(choices=MATCH_STATUS)
    local_team = models.ForeignKey(Club,related_name='local_team')
    visitor_team = models.ForeignKey(Club,related_name='visitor_name')
    f_referee = models.ForeignKey(Referee,blank=True,null=True,related_name='fm_referee',on_delete=models.SET_NULL)
    s_referee = models.ForeignKey(Referee,blank=True,null=True,related_name='sm_referee',on_delete=models.SET_NULL)
    local_goals = models.IntegerField(default=0)
    visitor_goals = models.IntegerField(default=0)
    #cards: green-yellow-red
    local_gc = models.IntegerField(default=0)
    local_yc = models.IntegerField(default=0)
    local_rc = models.IntegerField(default=0)
    visitor_gc = models.IntegerField(default=0)
    visitor_yc = models.IntegerField(default=0)
    visitor_rc = models.IntegerField(default=0)
    
class PlayerOnMatch(models.Model):
    player = models.ForeignKey(Player)
    match = models.ForeignKey(Match)
    team = models.ForeignKey(Team)
    goals = models.IntegerField()
    g_cards = models.IntegerField()
    y_cards = models.IntegerField()
    r_cards = models.IntegerField()
    
    class Meta:
        unique_together = ('player','match')
        
    def __unicode__(self):
        return self.player
    
class Standings(models.Model):
    tournament = models.ForeignKey(Tournament)
    team = models.ForeignKey(Team)
    played = models.IntegerField(default=0)
    win = models.IntegerField(default=0)
    draw = models.IntegerField(default=0)
    lost = models.IntegerField(default=0)
    favor_goals = models.IntegerField(default=0)
    against_goals = models.IntegerField(default=0)
    points = models.IntegerField(default=0)
    
class TournamentHistory(models.Model):
    name = models.CharField(max_length=45)
    tournamentYear = models.DateField()
    category = models.ForeignKey(Category,blank=True,null=True,on_delete=models.SET_NULL)
    regulation = models.TextField()
    champion = models.ForeignKey(Team)
    
    def __unicode__(self):
        return self.name    

class TournamentPhaseHistory(models.Model):
    name = models.CharField(max_length=30)
    tournament = models.ForeignKey(TournamentHistory)
    
    def __unicode__(self):
        return self.name
    
class MatchHistory(models.Model):
    category = models.ForeignKey(Category,blank=True,null=True,on_delete=models.SET_NULL)
    tournament = models.ForeignKey(TournamentHistory)
    tournament_phase = models.ForeignKey(TournamentPhaseHistory)
    match_date = models.DateField()
    match_time = models.TimeField()
    hockey_field = models.ForeignKey(HockeyField)
    local_team = models.ForeignKey(Team,related_name='local_team')
    visitor_team = models.ForeignKey(Team,related_name='visitor_name')
    fmh_referee = models.ForeignKey(Referee,blank=True,null=True,related_name='fmh_referee',on_delete=models.SET_NULL)
    smh_referee = models.ForeignKey(Referee,blank=True,null=True,related_name='smh_referee',on_delete=models.SET_NULL)
    local_goals = models.IntegerField(default=0)
    visitor_goals = models.IntegerField(default=0)
    #cards: green-yellow-red
    local_gc = models.IntegerField(default=0)
    local_yc = models.IntegerField(default=0)
    local_rc = models.IntegerField(default=0)
    visitor_gc = models.IntegerField(default=0)
    visitor_yc = models.IntegerField(default=0)
    visitor_rc = models.IntegerField(default=0)

class PlayerOnMatchHistory(models.Model):
    player = models.ForeignKey(Player)
    match = models.ForeignKey(MatchHistory)
    team = models.ForeignKey(Team)
    goals = models.IntegerField()
    g_cards = models.IntegerField()
    y_cards = models.IntegerField()
    r_cards = models.IntegerField()
        
    def __unicode__(self):
        return self.player

class StandingsHistory(models.Model):
    tournament = models.ForeignKey(TournamentHistory)
    team = models.ForeignKey(Team)
    played = models.IntegerField(default=0)
    win = models.IntegerField(default=0)
    draw = models.IntegerField(default=0)
    lost = models.IntegerField(default=0)
    favor_goals = models.IntegerField(default=0)
    against_goals = models.IntegerField(default=0)
    points = models.IntegerField(default=0)
    
#####################################################################
## FORMS

class ClubForm(ModelForm):
    class Meta:
        model = Club
        